﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MadLibrary.Collections;

namespace MadLibrary.Managing.Users
{
    /// <summary>
    /// Provides simple sortable collection of users.
    /// </summary>
    public class Users : Collection<User>
    {
        /// <summary>
        /// Creates new empty UserCollection object instance.
        /// </summary>
        public Users() : base()
        { 
            
        }
        /// <summary>
        /// Creates new UserCollection object instance and adds default users to collection.
        /// </summary>
        /// <param name="users">Specifies default users for current collection.</param>
        public Users(params User[] users) : base(users)
        {

        }
        /// <summary>
        /// Gets the user by the name specified.
        /// </summary>
        /// <param name="name">Specifies name of user which has to be selected.</param>
        /// <returns>User with name specified.</returns>
        public User this[string name]
        {
            get 
            {
                var res = from a in this.data where a.UserName == name select a;
                User b = null;
                foreach (User a in res)
                {
                    b = a;
                    break;
                }
                return b;
            }
            set
            {
                for (int i = 0; i < this.data.Length; i++)
                {
                    if (this.data[i].UserName == name)
                        this.data[i] = value;
                }
            }
        }
        /// <summary>
        /// Sorts this user collection by the specified option.
        /// </summary>
        /// <param name="config">Specifies configuration property for sorting.</param>
        public void Sort(SortConfiguration config)
        {
            if (config == SortConfiguration.Username)
                this.data = this.data.OrderBy(w => w.UserName).ToArray();
            else if (config == SortConfiguration.Email)
                this.data = this.data.OrderBy(w => w.Email).ToArray();
            else if (config == SortConfiguration.PasswordLength)
                this.data = this.data.OrderBy(w => w.Password.Length).ToArray();
        }
    }
}
